<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes"/>

<xsl:template match="/">
  <html>
  <head>
    <link rel="reference" href="http://matthewjamestaylor.com/blog/perfect-stacked-columns.htm" />
    <title>Hyper-V Host Report for <xsl:value-of select="HyperV/@Name" /></title>
    <link rel="stylesheet" HREF="HVReport-Screen.css" Media="Screen" TYPE="text/css" />
<!--    <link rel="stylesheet" HREF="HVReport-Print.css" Media="Print" TYPE="text/css" /> -->
  </head>
  <body>
  <div id="Header">
  <h1>Hyper-V Host Report for <xsl:value-of select="HyperV/@Name" /></h1>
  </div>
  <div class="colmask doublepage">
    <div class="colleft">
      <div class="col1"><xsl:apply-templates select="HyperV"/></div>
      <div class="col2"><xsl:apply-templates select="HyperV/Disks"/><xsl:apply-templates select="HyperV/CSVs"/></div>
    </div>
  </div>
  <xsl:apply-templates select="HyperV/VMs[@State='Running']" />
  <xsl:apply-templates select="HyperV/VMs[@State='Suspended']" />
  <xsl:apply-templates select="HyperV/VMs[@State='Stopped']" />
  <div id="Footer">
  <p>Hyper-V Reporter v0.2</p>
  </div>
  </body>
  </html>
</xsl:template>

<xsl:template match="HyperV">
  <div class="server">
    <table width="100%">
      <tr>
        <td class="srvimg"><h3>Host Configuration</h3><img src="images/host.png" class="srvimg" /></td>
        <td style="text-align: center;"><p>
          <xsl:choose>
            <xsl:when test="(@CPUs = 1)"><img src="images/cpuhost.png" class="icon" /></xsl:when>
            <xsl:when test="(@CPUs = 2)"><img src="images/cpuhost.png" class="icon" /><img src="images/cpuhost.png" class="icon" /></xsl:when>
            <xsl:when test="(@CPUs = 3)"><img src="images/cpuhost.png" class="icon" /><img src="images/cpuhost.png" class="icon" /><img src="images/cpuhost.png" class="icon" /></xsl:when>
            <xsl:when test="(@CPUs = 4)"><img src="images/cpuhost.png" class="icon" /><img src="images/cpuhost.png" class="icon" /><img src="images/cpuhost.png" class="icon" /><img src="images/cpuhost.png" class="icon" /></xsl:when>
          </xsl:choose></p>
          <p><xsl:value-of select="@CPUs"/> CPUs and <xsl:value-of select="@Threads"/> concurrent threads<br />CPUs are <xsl:value-of select="@CPUType"/></p>
          <hr />
          <p><xsl:choose>
            <xsl:when test="(@RAM &lt;= 8192)"><img src="images/ram.png" class="icon" /></xsl:when>
            <xsl:when test="(@RAM &lt;= 16384)"><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /></xsl:when>
            <xsl:when test="(@RAM &lt;= 32768)"><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /></xsl:when>
            <xsl:when test="(@RAM &lt;= 49152)"><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /></xsl:when>
            <xsl:when test="(@RAM &lt;= 65536)"><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /></xsl:when>
            <xsl:when test="(@RAM &gt; 65536)"><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /><img src="images/ram.png" class="icon" /></xsl:when>
          </xsl:choose></p>
        <p><xsl:value-of select="format-number(@RAM, '#,##0.0')"/> MB (<xsl:value-of select="format-number(@RAM div 1024, '#,##0.0')"/> GB) of RAM</p></td>
      </tr>
    </table>
  </div>
</xsl:template>


<xsl:template match="HyperV/Disks">
  <div class="storage">
    <h3>Local Disks</h3>
    <xsl:for-each select="Disk">
      <table style="width: 100%">
        <tr>
          <td class="iconcol"><img src="images/array1.png" class="icon" /></td>
          <td>
            <p style="vertical-align: top; text-align: left">Drive "<xsl:value-of select="@MountPoint" />" <xsl:if test="string-length(@Label)=0">(no label)</xsl:if><xsl:if test="not(string-length(@Label)=0)">(<xsl:value-of select="@Label" />)</xsl:if></p>
            <div class="progressbar">
              <xsl:element name="div">
                <xsl:attribute name="class">wrapper</xsl:attribute>
                <xsl:attribute name="style">width: <xsl:value-of select="@PctUsed" />%;</xsl:attribute>
                <xsl:element name="div">
                  <xsl:attribute name="class">light-on-dark</xsl:attribute>
                  <xsl:attribute name="style">width: <xsl:value-of select="format-number((10000 div @PctUsed), '0.0')" />%; background-color: <xsl:choose><xsl:when test="(@PctUsed &lt; 71)">#339933</xsl:when><xsl:when test="(@PctUsed &gt; 94)">#dd0000</xsl:when><xsl:otherwise>#E66C2C</xsl:otherwise></xsl:choose>;</xsl:attribute>
                  <span><xsl:value-of select="@PctUsed" />%</span>
                </xsl:element>
              </xsl:element>
              <div class="dark-on-light">
                  <span><xsl:value-of select="@PctUsed" />%</span>
              </div>
            </div>
            <p style="text-align: center"><span class="nobreaks"><xsl:value-of select="format-number(@Size, '#,##0.0')" /> GB total (<xsl:value-of select="format-number(@Free, '#,##0.0')" /> GB free)</span></p></td>
        </tr>
      </table>
      <xsl:if test="position()!=last()"><hr style=" " /></xsl:if>
    </xsl:for-each>
  </div> 
</xsl:template>


<xsl:template match="HyperV/CSVs">
  <div class="storage">
    <h3>Cluster Shared Volumes</h3>
    <xsl:for-each select="CSV">
      <table style="width: 100%">
        <tr>
          <td class="iconcol"><img src="images/array2.png" class="icon" /></td>
          <td>
            <p style="vertical-align: top; text-align: left">Mounted at "<xsl:value-of select="@MountPoint" />"</p>
            <div class="progressbar">
              <xsl:element name="div">
                <xsl:attribute name="class">wrapper</xsl:attribute>
                <xsl:attribute name="style">width: <xsl:value-of select="@PctUsed" />%;</xsl:attribute>
                <xsl:element name="div">
                  <xsl:attribute name="class">light-on-dark</xsl:attribute>
                  <xsl:attribute name="style">width: <xsl:value-of select="format-number((10000 div @PctUsed), '0.0')" />%; background-color: <xsl:choose><xsl:when test="(@PctUsed &lt; 71)">#339933</xsl:when><xsl:when test="(@PctUsed &gt; 94)">#dd0000</xsl:when><xsl:otherwise>#E66C2C</xsl:otherwise></xsl:choose>;</xsl:attribute>
                  <span><xsl:value-of select="@PctUsed" />%</span>
                </xsl:element>
              </xsl:element>
              <div class="dark-on-light">
                  <span><xsl:value-of select="@PctUsed" />%</span>
              </div>
            </div>
            <p style="text-align: center"><span class="nobreaks"><xsl:value-of select="format-number(@Size, '#,##0.0')" /> GB total (<xsl:value-of select="format-number(@Free, '#,##0.0')" /> GB free)</span></p></td>
        </tr>
      </table>
      <xsl:if test="position()!=last()"><hr style=" " /></xsl:if>
    </xsl:for-each>
  </div>
</xsl:template>

<xsl:template match="HyperV/VMs[@State='Running']">
  <h2>Running VMs</h2>
    <xsl:apply-templates select="VM">
      <xsl:sort select="VM/@Name" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="HyperV/VMs[@State='Suspended']">
  <h2>Suspended VMs</h2>
    <xsl:apply-templates select="VM">
      <xsl:sort select="VM/@Name" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="HyperV/VMs[@State='Stopped']">
  <h2>Stopped VMs</h2>
    <xsl:apply-templates select="VM">
      <xsl:sort select="VM/@Name" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="VM">
  <xsl:element name="div">
    <xsl:attribute name="class">vm<xsl:choose>
        <xsl:when test="(../@State = 'Stopped')"  > stopped</xsl:when>
        <xsl:when test="(../@State = 'Running')"  > running</xsl:when>
        <xsl:when test="(../@State = 'Suspended')"> suspended</xsl:when>
        <xsl:otherwise></xsl:otherwise>
      </xsl:choose></xsl:attribute>
    <div class="colmask blogstyle">
      <div class="colmid">
        <div class="colleft">
          <div class="col2">
            <h4><xsl:value-of select="@Name" /></h4>
            <p><xsl:value-of select="Description" /></p>
          </div>
          <div class="col1">
            <table >
              <xsl:element name="tr"><xsl:if test="(../@State = 'Running')">
                <xsl:choose>
                  <xsl:when test="(not(@CPULoad))">
                    <xsl:attribute name="class">ok</xsl:attribute>
                  </xsl:when>
                  <xsl:when test="(@CPULoad &lt; 71)">
                    <xsl:attribute name="class">ok</xsl:attribute>
                  </xsl:when>
                  <xsl:when test="(@CPULoad &lt; 91)">
                    <xsl:attribute name="class">warning</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="class">error</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose></xsl:if>
                <td class="iconcol"><img src="images/cpu.png" class="icon" /></td>
                <td><b><xsl:value-of select="@CPUs" /></b> Virtual CPU<xsl:if test="@CPUs &gt; 1">s</xsl:if></td>
              </xsl:element>
            </table>
            <hr style=" " />
            <table >
              <tr>
                <td class="iconcol"><img src="images/ram.png" class="icon" /></td>
                <td><b><xsl:value-of select="format-number(@RAM, '#,##0')"/> MB</b> (<xsl:value-of select="format-number(@RAM div 1024, '#,##0.0')"/> GB) of RAM</td>
              </tr>
            </table>
            <hr style=" " />
            <xsl:apply-templates select="Networks" />
          </div>
          <div class="col3">
              <xsl:apply-templates select="Disks/Disk" />
              <xsl:apply-templates select="Disks/DVD" />
            </div>
        </div>
      </div>
    </div>
  </xsl:element>
</xsl:template>

<xsl:template match="Networks">
    <xsl:for-each select="Network">
            <table >
              <xsl:element name="tr">
                <xsl:choose>
                  <xsl:when test="contains(@AdapterType, '100')">
                    <xsl:attribute name="class">warning</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="class">ok</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
                <td class="iconcol"><img src="images/nic.png" class="icon" /></td>
                <td><b>NIC <xsl:value-of select="position()" /></b><br/>
                    <xsl:value-of select="@AdapterType" /> NIC<br />vSwitch <b><xsl:value-of select="@vSwitch" /></b><xsl:choose><xsl:when test="(@VLAN &gt; 0)"> on
                    <b>VLAN <xsl:value-of select="@VLAN" /></b></xsl:when><xsl:otherwise> without VLANs</xsl:otherwise></xsl:choose><br />
                    MAC address <b><xsl:value-of select="@MAC" /></b></td>
              </xsl:element>
            </table>
      <xsl:if test="position()!=last()"><hr style=" " /></xsl:if>
    </xsl:for-each>
</xsl:template>

<xsl:template match="Disks/Disk">
            <table>
              <tr>
                <td class="iconcol"><img src="images/disk.png" class="icon" /><p style="margin-top: -4px;"><b>Disk <xsl:value-of select="position()"/></b></p></td>
                <td><b><xsl:value-of select="@DiskType" /><xsl:text> </xsl:text><xsl:value-of select="@MaxSize"/> GB disk</b> (<xsl:value-of select="@SizeOnDisk"/> GB allocated)<br />
                    Connected to <xsl:value-of select="@Controller"/><br />
                    Storage Path:<br /><xsl:call-template name="string-replace-all"><xsl:with-param name="text" select="@Location" /><xsl:with-param name="replace" select="'C:\ClusterStorage'" /><xsl:with-param name="by" select="'[CSV]'" /></xsl:call-template></td>
              </tr>
            </table>
      <xsl:if test="position()!=last()"><hr style=" " /></xsl:if>
</xsl:template>

<xsl:template match="Disks/DVD">
            <xsl:if test="position()=1"><hr style=" " /></xsl:if>
            <table>
              <tr>
                <td class="iconcol"><img src="images/dvd.png" class="icon" /><p style="margin-top: -4px;"><b>DVD <xsl:value-of select="position()"/></b></p></td>
                <td>Connected to <xsl:value-of select="@Controller"/><br />
                    <xsl:choose><xsl:when test="(string-length(@Location) &gt; 0)">Mapped to:<br /><xsl:call-template name="string-replace-all"><xsl:with-param name="text" select="@Location" /><xsl:with-param name="replace" select="'C:\ClusterStorage'" /><xsl:with-param name="by" select="'[CSV]'" /></xsl:call-template></xsl:when><xsl:otherwise>Not mapped to an ISO image or drive.</xsl:otherwise></xsl:choose></td>
              </tr>
            </table>
      <xsl:if test="position()!=last()"><hr style=" " /></xsl:if>
</xsl:template>


<!-- Helper Function - String Replace -->

<xsl:template name="string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text,$replace)" />
      <xsl:value-of select="$by" />
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="substring-after($text,$replace)" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template> 
</xsl:stylesheet> 